//------------------------------------------------------------------------------ 
//      Copyright (c) 2004, Be Nanashi-San 
// 
//      Permission is hereby granted, free of charge, to any person obtaining a 
//      copy of this software and associated documentation files (the "Software"), 
//      to deal in the Software without restriction, including without limitation 
//      the rights to use, copy, modify, merge, publish, distribute, sublicense, 
//      and/or sell copies of the Software, and to permit persons to whom the 
//      Software is furnished to do so, subject to the following conditions: 
// 
//      The above copyright notice and this permission notice shall be included in 
//      all copies or substantial portions of the Software. 
// 
//      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
//      IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
//      FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
//      AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
//      LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
//      FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
//      DEALINGS IN THE SOFTWARE. 
// 
//      File Name:   LocalDATReader.cpp
//      Author:      Be Nanashi-San 
//      Description: BBS Board list display class. 
//------------------------------------------------------------------------------
#include "config.h"
#include "config_user.h"
#ifdef USE_LOCALDAT

#include <OutlineListView.h>
#include <Directory.h>
#include <Entry.h>
#include <File.h>
#include <DataIO.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "BoardItem.h"
#include "LocalDATReader.h"
#include "NetUtil.h"
#include "Util.h"
#include "2chPositive.h"
#include "setting.h"

static const unsigned int kNAMEBUFSIZ = 256;

////////////////////////////////////////////////////////////////////////////////
// 
//
//
////////////////////////////////////////////////////////////////////////////////
LocalDATReader::LocalDATReader()
:BBSHandler("ローカルデータ")
{
}
////////////////////////////////////////////////////////////////////////////////
// 
//
//
////////////////////////////////////////////////////////////////////////////////
LocalDATReader::~LocalDATReader()
{
}

////////////////////////////////////////////////////////////////////////////////
// 
//
//
////////////////////////////////////////////////////////////////////////////////
status_t
LocalDATReader::BoardRequested(BMallocIO &buf)
{
	buf.SetSize(0);
	buf.Seek(0, SEEK_SET);
	this->localdatReader(buf);
	return B_OK;
}
////////////////////////////////////////////////////////////////////////////////
// 
//
//
////////////////////////////////////////////////////////////////////////////////
status_t
LocalDATReader::LoadFromLocal()
{
	this->BoardRequested(fBuf);
	return B_OK;
}
////////////////////////////////////////////////////////////////////////////////
// localdatReader
// ローカルキャッシュからツリーを作成。
//
////////////////////////////////////////////////////////////////////////////////
void
LocalDATReader::localdatReader(BMallocIO &buf)
 {
 BDirectory dir("/boot/home/config/settings/2chPositive/2chCache");
 BEntry ent,subent;
 BDirectory subdir,sub2dir;
 char strbuf[1024];
 int n;
 
	while(dir.GetNextEntry(&ent) == B_OK){
	 	if (subdir.SetTo(&ent) != B_OK )continue;
	 	char name[B_FILE_NAME_LENGTH];
	 	ent.GetName(name);
		n = sprintf(strbuf, "\n<BR><BR><B>%s</B><BR>\n", name);
		buf.Write(strbuf, n - 1);

	 	while(subdir.GetNextEntry(&subent) == B_OK){
	 		if (sub2dir.SetTo(&subent) != B_OK )continue;
	 		char subname[B_FILE_NAME_LENGTH];
	 		subent.GetName(subname);
			n = sprintf(strbuf, "<A HREF=http://%s/%s/>%s</A><br>\n", name, subname, subname);
			buf.Write(strbuf, n - 1);
		}
	} 

	n = sprintf(strbuf, "\n<BR><BR><B>おわり</B><BR>\n");
	buf.Write(strbuf, n - 1);
 }

////////////////////////////////////////////////////////////////////////////////
// 
//
//
////////////////////////////////////////////////////////////////////////////////
void
LocalDATReader::DrawBoardListReuested(BoardListView *owner, BMallocIO &buf)
{
	const char *s = static_cast<const char *>(buf.Buffer());
	size_t size = buf.BufferLength();
	this->defaultDrawer(owner, s, size, NULL, "おわり");
}
#endif
